package com.powergame.appstates;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.powergame.console.ExecuteOrdreCommand;
import com.powergame.console.NouveauJoueurCommand;
import com.powergame.console.NouvelOrdreCommand;
import com.powergame.console.SayHelloCommand;
import com.powergame.console.ShowCommand;
import com.powergame.console.StartGameCommand;
import com.powergame.controls.CameraControl;
import com.powergame.main.PowerGame2DMain;
import com.powergame.model.Joueur;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Console;
import de.lessvoid.nifty.controls.ConsoleCommands;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

/**
 * PlayScreenState.java
 *
 * Définit l'écran de jeu
 *
 * @author ToF
 */
public class PlayScreenState extends AbstractAppState implements ActionListener, ScreenController {
    
    private SimpleApplication application = null;
    private ViewPort viewPort;
    private Node rootNode;
    private Node guiNode;
    private AssetManager assetManager;
    private Node localRootNode = new Node("Play_RootNode");
    private Node localGuiNode = new Node("Play_GuiNode");
    
    // éléments de la scene 
    private Spatial sPlateau = null;
    
    public PlayScreenState(){
        //this(PowerGame2DMain.getInstance());
         this.application = PowerGame2DMain.getInstance();
    }
    
    public PlayScreenState(SimpleApplication app){
        this.application = PowerGame2DMain.getInstance();
        this.rootNode     = app.getRootNode();
        this.viewPort     = app.getViewPort();
        this.guiNode      = app.getGuiNode();
        this.assetManager = app.getAssetManager();  
        
        
        /** init the screen */    
        this.application.setDisplayFps(false);
        this.application.setDisplayStatView(false);
        this.application.getFlyByCamera().setEnabled(false);
        this.application.getFlyByCamera().setDragToRotate(true);
        
        // GUI
        /*NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, application.getInputManager(), app.getAudioRenderer(), app.getGuiViewPort());
        nifty = niftyDisplay.getNifty();
        nifty.fromXml("Interface/gui.xml", "start", this);
        app.getViewPort().addProcessor(niftyDisplay);
        */
        application.getInputManager().setCursorVisible(true);
        
        // Affiche le plateau
        //sPlateau = application.getAssetManager().loadModel("/Models/plateau.j3o");
        sPlateau = new Geometry("Plateau", new Box(Vector3f.ZERO, 112f, 1f, 112f));
        Material mat = new Material(application.getAssetManager(), "/Common/MatDefs/Light/Lighting.j3md");
        mat.setTexture("DiffuseMap", application.getAssetManager().loadTexture("/Maps/plateau_final.png"));
        sPlateau.setMaterial(mat); 
        
        sPlateau.setLocalTranslation(Vector3f.ZERO);
        localRootNode.attachChild(sPlateau);
         
        Node pieces = new Node("pieces");
        localRootNode.attachChild(pieces);
        
        // Lumières
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection((new Vector3f(0.5f, -0.5f, 0)).normalize());
        sun.setColor(ColorRGBA.White);
        localRootNode.addLight(sun);
        
        // Caméra
        application.getCamera().setLocation(new Vector3f(0, 200f, 0));
        application.getCamera().lookAt(Vector3f.ZERO, application.getCamera().getDirection());
        CameraControl camControl = new CameraControl(application.getCamera());
        camControl.registerInput(application.getInputManager());
        camControl.setxMax(200f);
        camControl.setxMin(-200f);
        camControl.setyMax(2000f);
        camControl.setyMin(-2000f);
        camControl.setzMin(-470f);
        camControl.setzMax(470f);
        sPlateau.addControl(camControl);
        
        initInputs();
    }

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
    }

    private void initInputs(){
        String[] mapping = {"MENU","DISPLAY_CONSOLE"};
        /*application.getInputManager().addMapping("SELECT", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        application.getInputManager().addMapping("MOVE", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
        application.getInputManager().addMapping("AQUIRE_ON_OFF", new KeyTrigger(KeyInput.KEY_M));
        application.getInputManager().addMapping("AQUIRE_NEXT_POLY", new KeyTrigger(KeyInput.KEY_N));
        application.getInputManager().addMapping("NV_JOUEUR", new KeyTrigger(KeyInput.KEY_J));
        */
        application.getInputManager().addMapping("MENU", new KeyTrigger(KeyInput.KEY_F10));
        application.getInputManager().addMapping("DISPLAY_CONSOLE", new KeyTrigger(KeyInput.KEY_F1));
        
        application.getInputManager().addListener(this, mapping);
    }

    @Override
    public void update(float tpf) {
        /** any main loop action happens here */
    }

    @Override
    public void stateAttached(AppStateManager stateManager) {
        rootNode.attachChild(localRootNode);
        guiNode.attachChild(localGuiNode);
    }

    @Override
    public void stateDetached(AppStateManager stateManager) {
        rootNode.detachChild(localRootNode);
        guiNode.detachChild(localGuiNode);
    }

    public void onAction(String name, boolean isPressed, float tpf) {
        if(name.equals("MENU") && !isPressed){
            ((PowerGame2DMain)application).menu();
        }
        if(name.equals("DISPLAY_CONSOLE") && !isPressed){
            Console console = ((PowerGame2DMain)application).nifty.getCurrentScreen().findNiftyControl("console", Console.class);
            console.setEnabled(!console.isEnabled());
            ((PowerGame2DMain)application).nifty.getCurrentScreen().findElementByName("hud").setVisible(console.isEnabled());
        }
    }

    @Override
    public void bind(Nifty nifty, Screen screen) {
        // Console
        Console console = screen.findNiftyControl("console", Console.class);
        
        console = screen.findNiftyControl("console", Console.class) ;
        
        ConsoleCommands consoleCommands = new ConsoleCommands(nifty, console);
        //consoleCommands.registerCommand("hello", new SayHelloCommand(console));
        consoleCommands.registerCommand("show", new ShowCommand((PowerGame2DMain)application));
        consoleCommands.registerCommand("nvjoueur", new NouveauJoueurCommand((PowerGame2DMain)application, console));
        consoleCommands.registerCommand("start", new StartGameCommand((PowerGame2DMain)application, console));
        consoleCommands.registerCommand("nvordre", new NouvelOrdreCommand((PowerGame2DMain)application));
        consoleCommands.registerCommand("exec", new ExecuteOrdreCommand((PowerGame2DMain)application));
        
        consoleCommands.enableCommandCompletion(true);
        
        console.output(consoleCommands.getRegisteredCommands() + "");
    }

    public void onStartScreen() {
    }

    public void onEndScreen() {
    }
}
